<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Session;
class PlayController extends Controller
{
    ////分配给各个页面的值
    //$video分给视频页的需要播放的视频 
    //$vcommet分配给视频页的评论信息
    //$poters分给vip页并且做判断的信息
   public function getIndex(Request $request)
	{
		$uid = session('uid');
		$id = $request->input('vid');


        $video = DB::table('video')
            ->where('id',$id)
            ->first();

         //视频点击,点击量加1
            $click = $video->click + 1;
            DB::table('video')
            ->where('id',$id)
            ->update(['click'=>$click]);

            
        //查询用户评论和对应用户头像姓名
        $vcomment = DB::table('vcomment')
        	->join('userdetail', 'vcomment.uid', '=', 'userdetail.uid')
            ->where('vcomment.vid',$id)
            ->orderBy('vcomment.id', 'desc')
            ->get();



		   	//推荐视频
		   $recommend = DB::table('video')
            ->where('tid',$video->tid)
            ->paginate(6);

            

            //添加到用户观看列表中
            if($request->session()->has('uid')){
            	
            //查询所有播放过的视频id
        	$search = DB::table('record')
	             ->select(DB::raw('vid'))
	             ->get();

	             foreach ($search as $k => $v) {
	               $res[]=$v->vid;
	             }

        	  //判断该视频是否已经观看
        	  if(!in_array($id,$res)){
        	  	DB::table('record')
        	  		->insert(['vid' => $id, 'uid' => session('uid')]);
        	  }      
            }

            $poters=DB::table('userdetail')
             ->where('uid','=',$uid)
             ->get();
            //========================================================================
               if(empty(session('uid'))&&($video->vips==1)){ 
               
               		return view('home.sign');
               }else if(($video->vips==1)&&!empty($poters[0]->vip)){
               
               //判断session是否存在查询收藏表,是否收藏
   		           if($request->session()->has('uid')){

   		            	//判断当前用户是否收藏该电影
   				   	$collection = DB::table('collection')
   				   		->where('vid',$id)
   				   		->where('uid',session('uid'))
   				   		->first();
   				   		return view('home.play.tvplay',['video'=>$video,'vcomment'=>$vcomment,'recommend'=>$recommend,'collection'=>$collection]);
   		            }else{
   		            	$collection = false;
   		            	return view('home.play.tvplay',['video'=>$video,'vcomment'=>$vcomment,'recommend'=>$recommend,'collection'=>$collection]);
   		            }
					
           	   }else if(($video->vips==1) && (empty($poters[0]->vip))){
           	   	
           		return view('home.viplogin',['poters'=>$poters]);
           	   }else if(($video->vips==0)){

           			//判断session是否存在查询收藏表,是否收藏
		           if($request->session()->has('uid')){

		            	//判断当前用户是否收藏该电影
				   	$collection = DB::table('collection')
				   		->where('vid',$id)
				   		->where('uid',session('uid'))
				   		->first();
				   		return view('home.play.tvplay',['video'=>$video,'vcomment'=>$vcomment,'recommend'=>$recommend,'collection'=>$collection]);
		            }else{
		            	$collection = false;
		            	return view('home.play.tvplay',['video'=>$video,'vcomment'=>$vcomment,'recommend'=>$recommend,'collection'=>$collection]);
		            }
           		}
            //========================================================================

             
	}

	//ajax把电影评论插入数据库
	public function getComment(Request $request)
	{

		$uid = session('uid');
		$vid = $request->input('vid');
		$comment = $request->input('comment');

		  $res = DB::table('vcomment')
		   		->insertGetId(['uid' => $uid, 'vid' => $vid,'message' =>$comment,'time'=>time()]);

		   	$userdetail = DB::table('userdetail')
		   		->where('uid',$uid)
		   		->first();

		if($res){

			echo view('home.play.ping',['comment'=>$comment,'userdetail'=>$userdetail,'time'=>time()]);
		}else{
			echo 0;
		}

	}

	//ajax把电影评论插入数据库
	public function getCollection(Request $request)
	{

		$uid = session('uid');
		$vid = $request->input('vid');
		$status = $request->input('status');

		 if($status == 0){
		 	//添加收藏
		 	  $res = DB::table('collection')
		   		->insert(['uid' => $uid, 'vid' => $vid]);
		 }else{
		 	//取消收藏
		 	$res = DB::table('collection')
		 		->where('uid',$uid)
		 		->delete();
		 }

		if($res){

			echo 1;
		}else{
			echo 0;
		}

	}

	//点赞方法
	public function getCount(){

		$count = $request->input('count')+1;
		$id = $request->input('id');
		$res = DB::table('vcomment')
		    ->where('id',$id)
		    ->update(['count'=>$count]);
		    
		if ($res) {
		    echo 1;
		}else{
		    echo 0;
		}
	}
}
